<script type="text/html" template lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-coreshopordermeeting-createForm" id="LAY-app-coreshopordermeeting-createForm">

        <!--<div class="layui-form-item">
        <label for="id" class="layui-form-label  layui-form-required">编号</label>
        <div class="layui-input-block">
            <input  type="number" min="0" max="999999" name="id"   lay-verType="tips" lay-verify="required|number" class="layui-input" value="1" placeholder="请输入编号" lay-reqText="请输入编号并为数字"  />
        </div>
    </div>-->

        <div class="layui-form-item">
            <label for="name" class="layui-form-label  layui-form-required">订货会名称</label>
            <div class="layui-input-block">
                <input name="name" lay-verType="tips" lay-verify="required" class="layui-input" placeholder="请输入订货会名称" lay-reqText="请输入订货会名称" />
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label for="brief" class="layui-form-label layui-form-required">描述</label>
            <div class="layui-input-block">
                <textarea name="describe" id="brief" lay-verify="required" class="layui-textarea" lay-reqText="描述内容" placeholder="描述内容"></textarea>
            </div>
        </div>



        <div class="layui-form-item">
            <label for="startdatetime" class="layui-form-label  layui-form-required">开始时间</label>
            <div class="layui-input-block">
                <input name="startdatetime" id="createTime-coreshopordermeeting-startdatetime" type="text" lay-verType="tips" lay-verify="required|datetime" class="layui-input" placeholder="请输入开始时间" lay-reqText="请输入开始时间" />
            </div>
        </div>

        <div class="layui-form-item">
            <label for="enddatetime" class="layui-form-label  layui-form-required">结束时间</label>
            <div class="layui-input-block">
                <input name="enddatetime" id="createTime-coreshopordermeeting-enddatetime" type="text" lay-verType="tips" lay-verify="required|datetime" class="layui-input" placeholder="请输入结束时间" lay-reqText="请输入结束时间" />
            </div>
        </div>

        <div class="layui-form-item">

            <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">封面图</a></legend></fieldset>-->

            <div class="layui-form-item">
                <label class="layui-form-label  layui-form-required" for="avatarImage">封面图：</label>
                <div class="layui-input-inline layui-inline-10">
                    <input name="image" id="fengmianImage" size="15" lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请输入【缩略图】" />
                </div>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="upBtn">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="viewImgBox" src="{{ layui.setter.noImagePicUrl }}">
                            <p id="viewTextBox"></p>
                        </div>
                    </div>
                </div>
            </div>

            <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">图集</a></legend></fieldset>-->
            <div class="layui-form-item">
                <div class="layui-input-inline layui-inline-2">
                    <input type="hidden" name="images" id="imageIds" class="layui-input" placeholder="请上传缩略图" lay-reqText="请上传缩略图" />
                    <button type="button" class="layui-btn  layui-btn-sm" id="upBtnimageIds"><i class="layui-icon"></i>上传图片</button>
                </div>
                <div class="layui-form-mid">
                    图集：[建议数量控制在10张以内]：
                </div>
            </div>
            <div class="layui-form-item">
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    <div class="layui-upload-list" id="upload-more-list"></div>
                </blockquote>
            </div>

            <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">媒体</a></legend></fieldset>
        <div class="layui-form-item">
            <label class="layui-form-label" for="video">视频：</label>
            <div class="layui-input-inline layui-inline-10">
                <input name="goods[video]" size="15" autocomplete="off" class="layui-input" lay-reqText="请输入【视频地址】" />
            </div>
            <div class="layui-form-mid">支持常用视频播放格式。</div>
        </div>-->
        </div>


        <div class="layui-form-item text-right core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-coreshopordermeeting-createForm-submit" id="LAY-app-coreshopordermeeting-createForm-submit" value="确认添加">
        </div>
    </div>
</script>


<script id="image_tpl" type="text/html">
    {{# layui.each(d, function(index, item){  }}
    <div class="imgdiv">
        <img src="{{ item }}" class="layui-upload-img" onclick='layui.coreHelper.viewImage("{{ item }}")' style="width: 100px;height:100px;">
        <div id="operate">
            <div><a class="del" onclick="delImg(this,'{{ item }}')">删除</a>|<a class="setmain" onclick="setDefault(this,'{{ item }}')">设为主图</a></div>
        </div>
    </div>
    {{#  }); }}
</script>


<script>
    var imgs = [];
    var debug= layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'laytpl', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg'],
        function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , laydate = layui.laydate
                , upload = layui.upload
                , laytpl = layui.laytpl
                , cropperImg = layui.cropperImg
                , coreHelper = layui.coreHelper;
	        
            laydate.render({
                elem: '#createTime-coreshopordermeeting-startdatetime',
                type: 'datetime'
            });
            laydate.render({
                elem: '#createTime-coreshopordermeeting-enddatetime',
                type: 'datetime'
            });
	        form.verify({
                
	        });
            //重载form
            form.render(null, 'LAY-app-coreshopordermeeting-createForm');

            //封面图图片上传
            $('#upBtn').click(function () {
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    imgSrc: $('#viewImgBox').attr('src'),
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                $('#viewImgBox').attr('src', res.data.fileUrl);
                                $("#fengmianImage").val(res.data.fileUrl);
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

            //图集上传
            $('#upBtnimageIds').click(function () {
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                imgs.push(res.data.fileUrl);
                                $('#imageIds').val(imgs);
                                var getTpl = image_tpl.innerHTML, view = document.getElementById('upload-more-list');
                                laytpl(getTpl).render(imgs, function (html) {
                                    view.innerHTML = html;
                                });
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

            //SKU缩略图上传
            $('.upSpecImage').click(function () {
                var _that = this;
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    imgSrc: 'no',
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                var item = $(_that);
                                item.prev().find('#productImagesInput').val(res.data.fileUrl);
                                item.prev().find('#productImagesSrc').attr("src", res.data.fileUrl);
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

          

            })

     

    };

    //删除图片
    function delImg(obj, imageUrl) {
        var imgDiv = $(obj).parent().parent().parent();
        imgDiv.remove();
        var index = imgs.indexOf(imageUrl);
        if (index > -1) {
            imgs.splice(index, 1);
        }
        $('#imageIds').val(imgs);
    }
    //设为默认图
    function setDefault(obj, imageUrl) {
        var imgDiv = $(obj).parent().parent().parent();
        $("#upload-more-list").prepend(imgDiv);
        var index = imgs.indexOf(imageUrl);
        if (index > -1) {
            imgs.unshift(imgs.splice(index, 1)[0]);
        }
        $('#imageIds').val(imgs);
    }
</script>
